package com.weng.db.demo;

import java.sql.*;
import java.util.List;

/**
* jdbc包路径：安装目录/Interface/jdbc/kingbase8jdbc.jar
* @author wengzhonghui
* @date: 2021-07-29 11:41:16
*/
public class KingbaseJDBC {

    public static void main(String[] args) {
        Connection con;
        String driver = "com.kingbase8.Driver";
        String jdbcUrl = "jdbc:kingbase8://172.16.0.74:3306/aep";
        String user = "root";
        String password = "root";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(jdbcUrl,user,password);
            if(!con.isClosed()){
                System.out.println("Succeeded connecting to the Database!");
            }

            List<String>  tableNames = DbUtil.getTableNames(jdbcUrl, "public",con.getMetaData());
            System.out.println(tableNames.size());

            Statement statement = con.createStatement();
            //要执行的SQL语句
            String sql = "select * from foo";
            //3.ResultSet类，用来存放获取的结果集！！
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("-----------------");
            System.out.println("执行结果如下所示:");
            System.out.println("-----------------");
            System.out.println("编号" + "\t" + "姓名");
            System.out.println("-----------------");

            String id = null;
            String name = null;
            while(rs.next()){
                id = rs.getString("A");

                System.out.println(id + "\t" + id);
            }
            rs.close();
            con.close();
        } catch(ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch(SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            System.out.println("数据库数据成功获取！！");
        }
    }

}
